/*
 * Copyright (C) 2010 TopCoder Inc., All Rights Reserved.
 */
package com.hercules.android.receiver.painter;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.hercules.android.receiver.Receiver;
import com.hercules.android.receiver.ReceiverUtil;
import com.hercules.android.receiver.entities.XRERectangle;
import com.hercules.android.receiver.entities.XREView;

/**
 * <p>
 * This class is a painter class which will draw a XRERectangle resource on a
 * canvas.
 * </p>
 * 
 * @author TCSASSEMBER
 * @version 1.0
 */
public class XRERectanglePainter extends XREPainter {
    /**
     * Constructor with a <code>Receiver</code> arguments.
     * 
     * @param receiver
     *            the <code>Receiver</code> instance.
     * @throws IllegalArgumentException
     *             if receiver if null
     */
    public XRERectanglePainter(Receiver receiver) {
        super(receiver);
    }

    /**
     * This method will paint the resource on canvas.
     * 
     * @param view
     *            the resource to paint
     * @param canvas
     *            the canvas that the resource painted on
     * @throws IllegalArgumentException
     *             if arguments is null
     * @throws XREPainterException
     *             if any error occurs
     */
    public void paint(XREView view, Canvas canvas) throws XREPainterException {
        // check the argument
        ReceiverUtil.checkNull("view", view);
        ReceiverUtil.checkNull("canvas", canvas);

        if (view.getResource() == 0) {
            return;
        }
        if (!view.getVisible() || !view.getPainting()) {
            return;
        }

        XRERectangle rectangle = (XRERectangle) receiver.getApplicationData().getElementsXREIdToElement().get(
                view.getResource());
        if (rectangle != null) {
            // fill the rectangle
            Paint fillpaint = new Paint();
            fillpaint.setColor(rectangle.getColor());
            fillpaint.setStyle(Paint.Style.FILL);
            fillpaint.setStrokeWidth(0.0f);
            fillpaint.setAlpha(ReceiverUtil.getNormalAlpha(view.getAlpha()));
            canvas.drawRect(0, 0, (float) view.getDimensions()[0], (float) view.getDimensions()[1], fillpaint);

            // draw board
            Paint boardpaint = new Paint();
            boardpaint.setColor(Color.BLUE);
            boardpaint.setAlpha(ReceiverUtil.getNormalAlpha(view.getAlpha()));
            float board = rectangle.getBorderThickness().floatValue();
            boardpaint.setStrokeWidth(board);
            boardpaint.setStyle(Paint.Style.STROKE);
            canvas.drawRect(0, 0, view.getDimensions()[0], view.getDimensions()[1], boardpaint);
        }
    }
}
